package com.sendinfo.common.lang;

import java.io.IOException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.digest.DigestUtils;

public abstract class TxtUtil {
	private static final String HEXES = "0123456789ABCDEF";

	/**
	 * 字符转成字节
	 * 
	 * @param c
	 * @return
	 */
	public static byte toByte(char c) {
		byte b = (byte) HEXES.indexOf(c);
		return b;
	}

	/**
	 * 合并数组
	 * 
	 * @param c
	 * @return
	 */
	public static List<Object> unionArray(Boolean repeat, Object[] ... arrays) {
		List<Object> result = new ArrayList<Object>();
		try {
			for(Object[] arr : arrays){
				for(Object ar : arr){
					if(repeat){
						if(!result.contains(ar)){
							result.add(ar);
						}
					}else{
						result.add(ar);
					}
				}
			}
		} catch (Exception e) {
			return null;
		}
		return result;
	}
	
	/**
	 * 字节数组转成HEX字符串（大写）
	 * 
	 * @param raw
	 * @return
	 */
	public static String toHex(byte[] raw) {
		if (raw == null) { return null; }
		final StringBuilder hex = new StringBuilder(2 * raw.length);
		for (final byte b : raw) {
			hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
		}
		return hex.toString();
	}
	/**
	 * 转换字符串到整数
	 * @author juror
	 * @date 2010-2-28 下午01:56:27
	 * @param raw
	 * @return
	 */
	public static int toInt(String value) {
		int result=0;
		try{
			result=Integer.valueOf(value);
		}catch(Exception e){
			
		}
		return result;
	}

	/**
	 * HEX字符串转成字节数组
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] toBytes(String hex) {
		int len = hex.length() / 2;
		hex = hex.toUpperCase();
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	/**
	 * 转成Unicode。
	 * 
	 * @param value
	 * @return
	 */
	public static String toUnicode(String value) {
		String result = "";
		String temp = "";
		try {
			for (char c : value.toCharArray()) {
				temp = "0000" + Integer.toHexString(c);
				temp = "\\u" + temp.substring(temp.length() - 4);
				result = result + temp;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param s
	 * @return
	 */
	public static final boolean isEmpty(String s) {
		return null == s || s.trim().isEmpty();
	}

	private static final String IP_PARTNER = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";

	public static final boolean isIPAddress(String s) {
		if (isEmpty(s)) return false;
		Pattern p = Pattern.compile(IP_PARTNER);
		Matcher m = p.matcher(s);
		return m.matches();
	}
	
	public static String getMd5(String data){
		return DigestUtils.md5Hex(data);
	}

	public static void main(String[] args) {
		System.out.println(getMd5("_input_charset=utf-8&body=ziuu&notify_url=http://localhost:8080/pay/alipayNotify.jspa&out_trade_no=7769110c-3761-4786-b61d-b94f626dc231&partner=2088102334784349&payment_type=1&paymethod=directPay&return_url=http://localhost:8080/pay/alipayReturn.jspa&seller_email=yanzi_ice@163.com&service=create_direct_pay_by_user&show_url=http://localhost:8080&subject=自游网&total_fee=0.01o06ii0ajfojo28t9tujmfzjpsh4q11tu "));
	}
	/**
	 * MD5加密
	 * 
	 * @param src
	 * @return
	 */
	public static final String digest(String src) {
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
			digest.update(src.getBytes("utf-8"));
			return toHex(digest.digest());
		} catch (Exception e) {
			return null;
		}
	}

	public static final String toHex(Serializable s) throws IOException {
		return toHex(RawUtil.serialize(s));
	}

	public static final Object fromHex(String hex) throws IOException, ClassNotFoundException {
		return RawUtil.deserialize(toBytes(hex));
	}

	/**
	 * 截取定长字符串。不足用字符代替
	 * 
	 * @author juror
	 * @date 2008-12-31 下午04:47:56
	 * @param value
	 * @param len
	 * @param c
	 * @return
	 */
	public static String align(String value, int len, char c, boolean leftAlign) {
		// TODO: to optimize.
		String result = "";
		if (leftAlign) for (int i = 0; i < len; i++) {
			if (i < value.length()) result = result + value.charAt(i);
			else result = result + c;
		}
		else for (int i = 0; i < len; i++) {
			if (i < value.length()) result = result + value.charAt(i);
			else result = c + result;
		}
		return result;
	}

	/**
	 * 按指定长度截取字符串（中英文都把一个字符长度处理）
	 * 
	 * @author:cshy Email:cshy486@126.com
	 * @Date:2009-8-12 下午01:34:49
	 * @param src
	 *            要截取的字符串
	 * @param len
	 *            要截取的长度（字节）
	 * @param end
	 *            结束字符串（如果截取后的字符串长度小于截取前字符串长在末尾添加的字符串）
	 * @return String 截取后的字符串
	 */
	public static String trim(String src, int len, String triming) {
		if (src == null || len <= 0) return "";
		byte[] bStr = src.getBytes();
		if (len >= bStr.length) return src;
		String cStr = new String(bStr, len - 1, 2);
		if (cStr.length() == 1 && src.contains(cStr)) len--;
		return new String(bStr, 0, len) + triming;
	}

	public static String replace(String src, int start, int len, String replacement) {
		if (src.length() < start + len - 1) { return src; }
		return new StringBuilder().append(src.substring(0, start - 1)).append(replacement).append(src.substring(start + len - 1))
				.toString();
	}

	/**
	 * 
	 * @Function:将字符集合按给定分隔符拼接成字符串
	 * @author:caoshiyan Email:cshy486@126.com
	 * @date:2009-3-3 下午03:48:45
	 * @param src
	 * @param c
	 * @return String
	 * 
	 */
	public static String connect(String[] src, String c) {
		if (src == null || src.length == 0) return "";
		StringBuffer sb = new StringBuffer("");
		sb.append(src[0]);
		for (int i = 1; i < src.length; i++)
			sb.append(c).append(src[i]);
		return sb.toString();
	}

	/**
	 * 字符串是否为有效邮件地址
	 * 
	 * @author:cshy Email:cshy486@126.com
	 * @Date:2009-10-30 下午05:46:20
	 * @param emailStr
	 * @return
	 */
	public static boolean isEmail(String emailStr) {		
		String regEmail = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		return TxtUtil.match(emailStr,regEmail, true);
	}

	/**
	 * 字符串是否为有效手机号码
	 * 
	 * @author:cshy Email:cshy486@126.com
	 * @Date:2009-10-30 下午05:46:18
	 * @param mobileStr
	 * @return
	 */
	public static boolean isMobile(String mobileStr) {
		//String regMobile = "(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}";
		String regMobile = "(13[0-9]|15[0-9]|18[0-9]|14[0-9])\\d{8}";
		return TxtUtil.match(mobileStr,regMobile, false);
	}
	
	/**
	 * 身份证号码
	 * @author juror
	 * @date 2010-6-17 下午06:42:29
	 * @param IDNumber
	 * @return
	 */
	public static boolean isIDNumber(String IDNumber){
        boolean result=IDNumber.matches("[0-9]{15}|[0-9]{17}[0-9X]");
        if(result){
            int year,month,date;
            if(IDNumber.length()==15){
                year=Integer.parseInt(IDNumber.substring(6,8));
                month=Integer.parseInt(IDNumber.substring(8,10));
                date=Integer.parseInt(IDNumber.substring(10,12));
            }
            else{
                year=Integer.parseInt(IDNumber.substring(6,10));
                month=Integer.parseInt(IDNumber.substring(10,12));
                date=Integer.parseInt(IDNumber.substring(12,14));
            }
            switch(month){
                case 2:result=(date>=1)&&(year%4==0?date<=29:date<=28);break;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:result=(date>=1)&&(date<=31);break;
                case 4:
                case 6:
                case 9:
                case 11:result=(date>=1)&&(date<=31);break;
                default:result=false;break;
            }
        }
        return result;
    }
	/**
	 * 正则匹配字符串判别
	 * 
	 * @author:cshy Email:cshy486@126.com
	 * @Date:2009-10-30 下午05:42:42
	 * @param reg
	 *            正则字符串
	 * @param src
	 *            要匹配目标字符串
	 * @param caseIgnore
	 *            是否忽略大小写
	 * @return boolean
	 */
	public static boolean match(String src, String reg, boolean caseIgnore) {
		Pattern pattern = caseIgnore ? Pattern.compile(reg, Pattern.CASE_INSENSITIVE) : Pattern.compile(reg);
		Matcher matcher = pattern.matcher(src);
		return matcher.matches();
	}
	
	/**
	 * 限制字符串长度，超出长度用str代替
	 * 
	 * @author:cshy Email:cshy486@126.com
	 * @Date:2009-10-30 下午05:42:42
	 * @param leng 限制长度
	 * @param src 目标字符串
	 * @param str 追加字符串
	 * @return String
	 */
	public static String preview(String src, int len, String str){
		if(src!=null&&src.length()>len){
			src = src.substring(0, len-1) + str;
		}
		return src;
	}
}
